-- 判断库存是否充足
local stockKey = KEYS[1]
local stock = tonumber(redis.call("get",stockKey))
if stock <= 0 then
    return 1
end

--库存充足，判断用户是否下单
local orderKey = KEYS[2]
local userId = ARGV[1]
local orderId = ARGV[2]
local voucherId = ARGV[3]
if redis.call("sismember",orderKey,userId) == 1 then
    return 2  --用户已下单
end

-- 用户未下单，扣减库存
redis.call("decr",stockKey)

--将userId存入当前优惠卷的set集合
redis.call("sadd",orderKey,userId)
--发送消息到队列中
redis.call("xadd","stream.orders","*", "userId",userId,"voucherId",voucherId , "id",orderId)
return 0




